package jscinttools.utilities.javautilities;
import java.lang.Double;
/**
 *  Some static methods for Core Java
 *
 *@author     Marc-Antoine Verdier
 *@created    27 janvier 2012
 */
public class JavaUtil{
	private JavaUtil() { }
	
	public static double valMax(double[] array){
		double val = Double.NEGATIVE_INFINITY;
		for (int i = 0; i < array.length; i++){
			if (array[i] > val && array[i] != Double.NaN)
				val = array[i];
		}
		return val;
	}
	
	public static double valMin(double[] array){
		double val = Double.POSITIVE_INFINITY;
		for (int i = 0; i < array.length; i++){
			if (array[i] < val && array[i] != Double.NaN)
				val = array[i];
		}
		return val;
	}
	
	public static double valAbove(double value, double[] array){
		double minVal = valMin(array);
		double maxVal = valMax(array);
		if (value <= minVal)
			return minVal;
		else if (value >= maxVal)
			return maxVal;
		else if (value == Double.NaN)
			return Double.NaN;
		else
			return calcAboveVal(value, array);
	}
	
	public static double calcAboveVal(double value, double[] array){
		double val = Double.POSITIVE_INFINITY;
		for (int i = 0; i < array.length; i++){
			if (array[i] > value  && array[i] <= val && array[i] != Double.NaN)
				val = array[i];
		}
		//System.out.println("==========>" + val);
		return val;
	}
	
	public static double valBelow(double value, double[] array){
		double minVal = valMin(array);
		double maxVal = valMax(array);
		if (value <= minVal)
			return minVal;
		else if (value >= maxVal)
			return maxVal;
		else if (value == Double.NaN)
			return Double.NaN;
		else
			return calcBelowVal(value, array);
	}
	
	public static double calcBelowVal(double value, double[] array){
		double val = Double.NEGATIVE_INFINITY;
		for (int i = 0; i < array.length; i++){
			if (array[i] < value  && array[i] >= val && array[i] != Double.NaN)
				val = array[i];
		}
		return val;
	}
	
	/**
	 *  Calculate the interval width surounding a value for an array of sorted increasing intervals
	 *
	 *@param  val	The value to suround
	 *@param  array	The array of intervals
	 *@return	The width of the intervals surounding the value
	 */
	public static double intervalWidth(double value, double[] array){
		double minVal = valMin(array);
		double maxVal = valMax(array);
		//System.out.println("##########################");
		//System.out.println(minVal);
		//System.out.println(maxVal);
		if (value == minVal){
			//System.out.println("Kawabunga");
			return calcAboveVal(value, array) - minVal;
		}
		else if (value == maxVal)
			return Double.NaN;
		else if (value < maxVal && value > minVal && value != Double.NaN)
			return findIntervalWidthSorted(value, array);
		else
			return Double.NaN;
	}
	
	private static double findIntervalWidthSorted(double value, double[] array){
		double aboveVal = valAbove(value, array);
		double belowVal = valBelow(value, array);
		//System.out.println("Kawabunga");
		//System.out.println(value);
		//System.out.println(aboveVal);
		//System.out.println(belowVal);
		//System.out.println(aboveVal - belowVal);
		return aboveVal - belowVal;
	}
}
